package service

import (
	"fmt"
	"kim/config"
	"kim/model"
	"time"
)

func AddUserToGroup(userId, groupId int64) {

	relation := &model.Relation{
		UserId:     userId,
		GroupId:    groupId,
		CreateTime: time.Now(),
	}

	result := config.DB.Create(&relation)
	if result.Error != nil {
		fmt.Println(result.Error)
	}

}

func GetGroupsByUserId(userId int64) []int64 {

	queryWrapper := &model.Relation{
		UserId: userId,
	}
	//var ans []model.Relation
	ans := []model.Relation{}

	config.DB.Where(queryWrapper).Find(&ans)

	groupIds := []int64{}
	for _, relation := range ans {

		fmt.Println(relation.GroupId)
		groupIds = append(groupIds, relation.GroupId)
	}

	fmt.Println(groupIds)
	return groupIds

}
